<?php
require 'TVB/pms/pdes/common/bootstrap.php';
/**
 * Parameters: (required)  [optional]
 *
 * Example: get_folders.php?action=folder_list&type=programme&id=2
 *
 * @param $id				  INT		($programme_id | $episode_id | $cast_id | $video_id)
 * @param $type				  STRING	(programme | episode | cast | video)
 * @param $folder_type		  INT		[$folder_type_id]
 * @param $folder_item_type   STRING	(video | image)
 * 
 */

if(empty($_REQUEST['id']) || empty($_REQUEST['type']) || empty($_REQUEST['folder_item_type'])){
	exit('[]');
}

$type = strtolower($_REQUEST['type']);
$id   = (int)$_REQUEST['id'];
$item_type = strtolower($_REQUEST['folder_item_type']);

if(!in_array($type,array('programme','episode','cast','video')) || $id<1 || !in_array($item_type,array('image','video')) ){
	exit('[]');
}

$folders = Model::selectBySql("select folders from $type where id=$id");
if(empty($folders[0]['folders'])) exit('[]');
$folders = $folders[0]['folders'];

if($item_type == 'video'){
	$watermark = '';
	$folder_item_id_sql = ' AND folder_item_type_id='.AC::FOLDER_TYPE_VIDEO;
}else{
	$watermark = 'IFNULL((SELECT watermark_id FROM `image_folder-config` WHERE folder_id=f.id),0) AS watermark,';
	$folder_item_id_sql = ' AND folder_item_type_id='.AC::FOLDER_TYPE_IMAGE;
}

$en_lan_id = language::getLanguageID(AC::LANGUAGE_EN);
$zh_lan_id = language::getLanguageID(AC::LANGUAGE_ZH);
$sql = "
SELECT 
	id,
	IFNULL((SELECT title FROM `folder-language` WHERE language_id=$en_lan_id AND folder_id = f.id),'') AS title_en,
	IFNULL((SELECT title FROM `folder-language` WHERE language_id=$zh_lan_id AND folder_id = f.id),'') AS title_cn,
	CASE WHEN EXISTS (SELECT id FROM folder_item WHERE folder_id = f.id) THEN 1 ELSE 0 END AS has_item,
	$watermark
	folder_type_id AS folder_type
FROM folder f WHERE id IN($folders) $folder_item_id_sql
";

#echo $sql;
$data = Model::selectBySql($sql);
foreach ($data as $key1=>$value1) {
	$data[$key1]['title_en'] = htmlentities($value1['title_en'], ENT_QUOTES, 'UTF-8');
	$data[$key1]['title_cn'] = htmlentities($value1['title_cn'], ENT_QUOTES, 'UTF-8');
}
/*
$data = array(
	array('id' => '1', 'title_en' => 'aaaaaaaaaaaa', 'title_cn' => '啊', 'has_item' => '0', 'watermark'=>'1', 'folder_type'=>'1'),
	array('id' => '2', 'title_en' => 'bbbbbbbbbbbbbbbb', 'title_cn' => '吧', 'has_item' => '1', 'watermark'=>'1', 'folder_type'=>'2'),
	array('id' => '3', 'title_en' => 'ccccccccccc', 'title_cn' => '才', 'has_item' => '1', 'watermark'=>'0', 'folder_type'=>'3'),
	array('id' => '4', 'title_en' => 'dddddddddd', 'title_cn' => '的', 'has_item' => '0', 'watermark'=>'1', 'folder_type'=>'4'),
	array('id' => '5', 'title_en' => 'eeeeeeeeeeeee', 'title_cn' => '餓', 'has_item' => '0', 'watermark'=>'1', 'folder_type'=>'5'),
	array('id' => '6', 'title_en' => 'ffffffffffff', 'title_cn' => '飛', 'has_item' => '1', 'watermark'=>'0', 'folder_type'=>'6'),
	array('id' => '7', 'title_en' => 'ggggggggggggg', 'title_cn' => '個', 'has_item' => '0', 'watermark'=>'1', 'folder_type'=>'7'),
	array('id' => '8', 'title_en' => 'hhhhhhhhhh', 'title_cn' => '好', 'has_item' => '0', 'watermark'=>'1', 'folder_type'=>'8'),
	array('id' => '9', 'title_en' => 'iiiiiiii', 'title_cn' => '一', 'has_item' => '1', 'watermark'=>'1', 'folder_type'=>'1'),
	array('id' => '10', 'title_en' => 'jjjjjjjjjjjjjjjjjj', 'title_cn' => '酒','has_item' => '0',  'watermark'=>'0', 'folder_type'=>'3')
);
 */
echo json_encode($data);

?>
